import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xin_nuo_pda/bean/bin_info_entity.dart';
import 'package:xin_nuo_pda/utils/string_extension.dart';
import 'package:xin_nuo_pda/values/colors.dart';
import 'package:xin_nuo_pda/values/constant.dart';
import 'package:xin_nuo_pda/views/SubTitleBar.dart';
import 'package:xin_nuo_pda/views/TextView.dart';
import 'package:xin_nuo_pda/views/edittext/TitleEditText.dart';

import 'DefectClearThingController.dart';

class DefectClearThingPage extends StatelessWidget {
  final controller = Get.put(DefectClearThingController());
  final state = Get
      .find<DefectClearThingController>()
      .state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(USER),
      ),
      body: SingleChildScrollView(
        child: Container(
          child: Column(
            children: [
              SubTitleBar("清料", '提交', () {
                if(state.lotEntity == null){
                  Get.snackbar('提示', '未获取到批次数据');
                  return;
                }

                if(state.binData == null){
                  Get.snackbar('提示', '未获取到BIN数据');
                  return;
                }
                controller.clearThing();
              }),
              TitleEditText(
                "批次ID:",
                "请扫描批次ID",
                    (value) {
                  controller.getLotInfo(value);
                },
              ),
              GetBuilder<DefectClearThingController>(
                builder: (controller) =>
                    Column(
                      children: [
                        TextView(
                            "设备ID(主):", state.lotEntity?.eQUIPMENTID ?? ''),
                        TextView("设备ID(辅):", ""),
                        TextView("批次数量:",
                            state.lotEntity?.mAINQTY.toString() ?? '0'),
                        Table(
                          defaultVerticalAlignment:
                          TableCellVerticalAlignment.middle,
                          columnWidths: {
                            //列宽
                            0: FlexColumnWidth(2.0),
                            1: FlexColumnWidth(1.0),
                            2: FlexColumnWidth(1.0),
                            3: FlexColumnWidth(2.0),
                          },
                          border: TableBorder.all(
                            color: Colors.grey,
                            width: 1.0,
                            style: BorderStyle.solid,
                          ),
                          children: getTableList(context),
                        )
                      ],
                    ), id: state.defectClear,),
            ],
          ),
        ),
      ),
    );
  }

  List<TableRow> getTableList(BuildContext context) {
    List<TableRow> tempView = [];
    List<FocusNode> focusNodeList = [];
    tempView.add(TableRow(
        children: [
          //增加行高
          SizedBox(
            height: 40,
            child: Align(
              alignment: Alignment.center,
              child: Text(
                '批号',
                style: TextStyle(
                  color: theme_blue.toColor(),
                ),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              'BIN名称',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '数量',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '动作类型',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
        ],
        //第一行样式 添加背景色
        decoration: BoxDecoration(
          color: table_head.toColor(),
        )));
    if (state.binData == null) {
      state.binData = [];
    }
    for (int i = 0; i < state.binData!.length; i++) {
      BinInfo element = state.binData![i];
      focusNodeList.add(FocusNode());
      tempView.add(TableRow(children: [
        //增加行高
        SizedBox(
          height: 40,
          child: Align(
            alignment: Alignment.center,
            child: Text(
              state.lotEntity!.lOTID ?? '',
              style: TextStyle(
                color: text_black.toColor(),
              ),
            ),
          ),
        ),
        Align(
          alignment: Alignment.center,
          child: Text(
            element.BINNAME ?? '',
            style: TextStyle(
              color: text_black.toColor(),
            ),
          ),
        ),
        Align(
          alignment: Alignment.center,
          child: Text(
            element.MAINQTY.toString(),
            style: TextStyle(
              color: text_black.toColor(),
            ),
          ),
        ),
        GetBuilder<DefectClearThingController>(builder: (controller) {
          return Align(
            alignment: Alignment.center,
            child: Expanded(
                flex: 7,
                child: Container(
                  padding: EdgeInsets.only(left: 8),
                  height: 36,
                  decoration: BoxDecoration(
                      border: new Border.all(
                          width: 1.0, color: Colors.grey),
                      borderRadius:
                      BorderRadius.all(Radius.circular(8))),
                  child: DropdownButtonHideUnderline(
                    child: DropdownButton(
                      value: element.CLEARTYPE ?? '',
                      isExpanded: true,
                      items: [
                        DropdownMenuItem(
                          child: Text('报废'),
                          value: "Scrap",
                        ),
                        DropdownMenuItem(
                          child: Text('留站内'),
                          value: "None",
                        ),
                        DropdownMenuItem(
                          child: Text('返工'),
                          value: "Rework",
                        ),
                        DropdownMenuItem(
                          child: Text('分批'),
                          value: "Split",
                        ),
                        DropdownMenuItem(
                          child: Text('分批出站'),
                          value: "SplitTrackOut",
                        ),
                        DropdownMenuItem(
                          child: Text('分批报废'),
                          value: "SplitScrap",
                        ),
                      ],
                      onChanged: (value) {
                        controller.updateSelectDefectValue(
                            value.toString(),i);
                      },
                    ),
                  ),
                )),
          );
        },id: state.dropDownId,),
      ]));
    }
    return tempView;
  }
}
